/**
 * 导出异步
 */
import React from "react"
import fetch from "fetch/fetch"
import ModalTip from "modalTip"
let ExportAsync = function() {}

let intervalId = ""

ExportAsync.exportAsync = function(urlPath, params) {
    window.clearInterval(intervalId)
    fetch
        .post(urlPath, params)
        .then(res => {
            intervalId = window.setInterval(function() {
                ExportAsync.getDownloadPath(res.querykey)
            }, 1000 * 3)
        })
        .catch(data => {
            ModalTip.warningTip(data.msg)
        })
}

ExportAsync.getDownloadPath = function(queryKey) {
    fetch
        .post("/export/queryExportResult", { queryKey: queryKey })
        .then(res => {
            if (res.downloadPath != null) {
                document.getElementById("down_load").setAttribute("src", res.downloadPath)
                window.clearInterval(intervalId)
            }
        })
        .catch(data => {
            ModalTip.warningTip(data.msg)
        })
}

export default ExportAsync
